package cn.openea.perpartner.cms.web.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.openea.perpartner.cms.factory.UserContext;
import cn.openea.perpartner.cms.web.dto.AssignDto;
import cn.openea.perpartner.cms.web.entity.CmsSales;
import cn.openea.perpartner.cms.web.entity.CmsSalesAssistant;
import cn.openea.perpartner.cms.web.mapper.CmsSalesMapper;
import cn.openea.perpartner.cms.web.service.ICmsSalesAssistantService;
import cn.openea.perpartner.cms.web.service.ICmsSalesService;
import cn.openea.perpartner.system.util.ToolsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 销售成员表(CmsSales)表服务实现类
 *
 * @author makejava
 * @since 2022-08-16 14:23:06
 */
@Service("cmsSalesService")
public class CmsSalesServiceImpl extends ServiceImpl<CmsSalesMapper, CmsSales> implements ICmsSalesService {

    @Autowired
    private ICmsSalesAssistantService assistantService;
    @Override
    public List<CmsSales> selectAll(String userId, String searchStr) {
        LambdaQueryWrapper<CmsSales> wrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotBlank(userId)) {
            wrapper.eq(CmsSales::getAssistantId, userId);
        }
        if (StrUtil.isNotBlank(searchStr)) {
            wrapper.and(wrap -> wrap.like(CmsSales::getOpeneaId, searchStr)
                    .or().like(CmsSales::getUserName, searchStr)
                    .or().like(CmsSales::getPost, searchStr)
                    .or().like(CmsSales::getEducation, searchStr)
                    .or().like(CmsSales::getPhone, searchStr)
                    .or().like(CmsSales::getEntryTime, searchStr)
                    .or().like(CmsSales::getAge, searchStr));
        }
        wrapper.orderByDesc(CmsSales::getCreateTime);
        return list(wrapper);
    }
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    @Override
    public boolean insert(CmsSales sales) {
        if (StrUtil.isBlank(sales.getOpeneaId())){
            sales.setOpeneaId(ToolsUtil.autoUserID());
        }
        sales.setCreateUser(UserContext.getUserId());
        sales.setUpdateUser(UserContext.getUserId());
        sales.setAssistantId(UserContext.getUserId());
        sales.setAssistantName(UserContext.getUserName());
        return save(sales);
    }
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    @Override
    public boolean assign(List<AssignDto> assignList) {
        boolean isSuccess = false;
        for (AssignDto dto:assignList){
            System.out.println(dto);
            CmsSales sales = baseMapper.selectById(dto.getAssignId());
            sales.setAssistantId(dto.getUserId());
            sales.setAssistantName(dto.getUserName());
            isSuccess =  updateById(sales) ;
        }
        return isSuccess;
    }

    @Override
    public boolean hired(List<Long> idList) {
        //1、根据idList查询销售
        LambdaQueryWrapper<CmsSales> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(CmsSales::getSalesId,idList);
        List<CmsSales> salesList = list(wrapper);
        //2、将查询到的销售，复制到销售助理表
        List<CmsSalesAssistant> assistantList = new ArrayList<>();
        salesList.forEach(sales->{
            CmsSalesAssistant assistant = new CmsSalesAssistant();
            BeanUtil.copyProperties(sales,assistant);
            assistantList.add(assistant);
        });
        return assistantService.saveBatch(assistantList);
    }


}

